﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace RuntimeConsole
{
    class ControlPanel : MonoBehaviour
    {
        public RuntimeConsole console;

        readonly string StoreKey = "filterRegex";
        InputField filterRegexInput;

        private void Start()
        {
            filterRegexInput = FindChildren(gameObject, "FilterRegex").GetComponent<InputField>();
            filterRegexInput.text = PlayerPrefs.GetString(StoreKey, "");

            AddClick(gameObject, "Close", Close);
            AddClick(gameObject, "Clear", Clearlog);
            AddClick(gameObject, "Filter", Setfilter);
            AddClick(gameObject, "ClearRegex", ClearRegex);

        }

        private void Close()
        {
            console.Showing = false;
        }
        private void Clearlog()
        {
            console.logs.Clear();
            console.filterLogs.Clear();
        }
        private void Setfilter()
        {
            console.SetFilter(filterRegexInput.text);
            PlayerPrefs.SetString(StoreKey, filterRegexInput.text);
        }
        private void ClearRegex()
        {
            filterRegexInput.text = "";
           console.SetFilter();
        }

        private void AddClick(GameObject go, string childrenName, UnityAction call)
        {
            FindChildren(go, childrenName).GetComponent<Button>().onClick.AddListener(call);
        }

        private GameObject FindChildren(GameObject go, string childrenName)
        {
            Transform t = go.transform.Find(childrenName);
            if (t == null)
            {
                return null;
            }
            return t.gameObject;
        }
    }
}
